<!DOCTYPE html>
<html lang="en">
<head><!-- hexo injector head_begin start -->
        <link rel="preconnect" href="https://blog.fantasyke.cn/picx-images-hosting" crossorigin=""><!-- hexo injector head_begin end -->
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/uploads/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/uploads/avatar.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="google-site-verification" content="0qBBGdG7vYORyT5XLZRUqQZ_y5kKYhgyUwNqjSIImg0">
  <meta name="msvalidate.01" content="63C90D0EC3DA6C31FD109132B7BEF502">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Zen+Maru+Gothic:300,300italic,400,400italic,700,700italic%7CZCOOL+XiaoWei:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" integrity="sha256-5eIC48iZUHmSlSUz9XtjRyK2mzQkHScZY1WdMaoz74E=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.31/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.fantasyke.cn","root":"/","images":"/images","scheme":"Mist","darkmode":false,"version":"8.21.1","exturl":true,"sidebar":{"position":"right","width_expanded":350,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":null},"fold":{"enable":true,"height":500},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":true,"nav":{"gitalk":{"text":"gitalk","order":-1}},"activeClass":"gitalk"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="主流消息中间件 ActiveMQ Kafka">
<meta property="og:type" content="article">
<meta property="og:title" content="RabbitMQ基础">
<meta property="og:url" content="https://blog.fantasyke.cn/posts/33842/index.html">
<meta property="og:site_name" content="Fantasy&#39;Ke Blog">
<meta property="og:description" content="主流消息中间件 ActiveMQ Kafka">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.5fkly2609k.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.syyxd7efm.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.3uuuyl8nv6.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.9dczeqgtfh.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.1lbuf3nraq.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.86to64rpy0.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.7w6uczc9lq.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.4qrce1j5os.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.3nrn35ngns.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.7zqgap6y0v.webp">
<meta property="og:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.491apgi7ud.webp">
<meta property="article:published_time" content="2023-05-26T14:36:00.000Z">
<meta property="article:modified_time" content="2024-12-17T06:36:35.389Z">
<meta property="article:author" content="Fantasy-ke">
<meta property="article:tag" content="消息队列">
<meta property="article:tag" content="RabbitMQ">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.5fkly2609k.webp">


<link rel="canonical" href="https://blog.fantasyke.cn/posts/33842/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"https://blog.fantasyke.cn/posts/33842/","path":"posts/33842/","title":"RabbitMQ基础"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>RabbitMQ基础 | Fantasy'Ke Blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Fantasy'Ke Blog" type="application/atom+xml">
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Fantasy'Ke Blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="Search" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a></li><li class="menu-item menu-item-photos"><a href="/photos/" rel="section"><i class="fas fa-camera-retro fa-fw"></i>photos</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="Searching..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">1.</span> <span class="nav-text"> 主流消息中间件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#-2"><span class="nav-number">1.1.</span> <span class="nav-text"> ActiveMQ</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-3"><span class="nav-number">1.2.</span> <span class="nav-text"> Kafka</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-4"><span class="nav-number">1.3.</span> <span class="nav-text"> RocketMQ</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-5"><span class="nav-number">1.4.</span> <span class="nav-text"> RabbitMQ</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-6"><span class="nav-number">2.</span> <span class="nav-text"> RabbitMQ核心概念及AMQP协议</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#-7"><span class="nav-number">2.1.</span> <span class="nav-text"> 为什么用RabbitMQ</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-8"><span class="nav-number">2.2.</span> <span class="nav-text"> RabbitMQ高性能的原因</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-9"><span class="nav-number">2.3.</span> <span class="nav-text"> 什么是AMQP</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-10"><span class="nav-number">2.4.</span> <span class="nav-text"> AMQP核心概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-11"><span class="nav-number">2.5.</span> <span class="nav-text"> RabbitMQ整体架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-12"><span class="nav-number">2.6.</span> <span class="nav-text"> RabbitMQ消息流转</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-13"><span class="nav-number">3.</span> <span class="nav-text"> RabbitMQ常用命令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#-14"><span class="nav-number">3.1.</span> <span class="nav-text"> rabbitmq-server</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-15"><span class="nav-number">3.2.</span> <span class="nav-text"> rabbitmqctl</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#-16"><span class="nav-number">3.2.1.</span> <span class="nav-text"> 用户相关</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-17"><span class="nav-number">3.2.2.</span> <span class="nav-text"> virtual host相关</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-18"><span class="nav-number">3.2.3.</span> <span class="nav-text"> queue相关</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#-19"><span class="nav-number">3.2.4.</span> <span class="nav-text"> 高级操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-20"><span class="nav-number">3.3.</span> <span class="nav-text"> rabbitmq-plugins</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-21"><span class="nav-number">4.</span> <span class="nav-text"> 消息生产与消费</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#-22"><span class="nav-number">4.1.</span> <span class="nav-text"> 重要概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-23"><span class="nav-number">4.2.</span> <span class="nav-text"> 创建项目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-24"><span class="nav-number">4.3.</span> <span class="nav-text"> 具体代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-25"><span class="nav-number">4.4.</span> <span class="nav-text"> 运行测试</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-26"><span class="nav-number">5.</span> <span class="nav-text"> Exchange–交换机</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#-27"><span class="nav-number">5.1.</span> <span class="nav-text"> Exchange属性(交换机属性)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-28"><span class="nav-number">5.2.</span> <span class="nav-text"> Direct Exchange</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#-29"><span class="nav-number">5.2.1.</span> <span class="nav-text"> 代码示例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#-30"><span class="nav-number">5.2.1.1.</span> <span class="nav-text"> 创建项目</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-31"><span class="nav-number">5.2.1.2.</span> <span class="nav-text"> 具体代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-32"><span class="nav-number">5.3.</span> <span class="nav-text"> Topic Exchange</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#-33"><span class="nav-number">5.3.1.</span> <span class="nav-text"> 代码示例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#-34"><span class="nav-number">5.3.1.1.</span> <span class="nav-text"> 创建项目</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-35"><span class="nav-number">5.3.1.2.</span> <span class="nav-text"> 具体代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#-36"><span class="nav-number">5.4.</span> <span class="nav-text"> Fanout Exchange</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#-37"><span class="nav-number">5.4.1.</span> <span class="nav-text"> 代码示例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#-38"><span class="nav-number">5.4.1.1.</span> <span class="nav-text"> 创建项目</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#-39"><span class="nav-number">5.4.1.2.</span> <span class="nav-text"> 具体代码</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-40"><span class="nav-number">6.</span> <span class="nav-text"> Binding–绑定</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-41"><span class="nav-number">7.</span> <span class="nav-text"> Queue–消息队列</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-42"><span class="nav-number">8.</span> <span class="nav-text"> Message–消息</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#-43"><span class="nav-number">9.</span> <span class="nav-text"> Virtual Hots–虚拟主机</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Fantasy-ke"
      src="/uploads/avatar.png">
  <p class="site-author-name" itemprop="name">Fantasy-ke</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">36</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnRhc3kta2U=" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;fantasy-ke"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly94LmNvbS9BX0RyZWFtZXJfSGFyZA==" title="Twitter → https:&#x2F;&#x2F;x.com&#x2F;A_Dreamer_Hard"><i class="fab fa-twitter fa-fw"></i>Twitter</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly93d3cueW91dHViZS5jb20vQCVFNSU5MSVBOCVFNiVBMiVBNi16OWk=" title="YouTube → https:&#x2F;&#x2F;www.youtube.com&#x2F;@%E5%91%A8%E6%A2%A6-z9i"><i class="fab fa-youtube fa-fw"></i>YouTube</span>
      </span>
  </div>
<div class="cc-license animated" itemprop="sponsor">
  <a href="https://www.netlify.com" class="cc-opacity" title="Deploy with Netlify → https://www.netlify.com" target="_blank"><img width="80" src="https://www.netlify.com/img/global/badges/netlify-dark.svg" alt="Netlify"></a>
</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.css">
<div id="aplayer" style="position:relative;left;0;bottom:0;"></div>
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/color-thief-don@2.0.2/src/color-thief.js"></script>
<script>
  const ap = new APlayer({
    container: document.getElementById('aplayer'),
    autoplay: false, //自动播放
    listFolded: true, //播放列表默认折叠
    listMaxHeight: 90, //播放列表最大高度
    order: 'list', //音频循环顺序, 可选值: 'list', 'random'
    loop: 'all', //音频循环播放, 可选值: 'all', 'one', 'none'
    theme: '#e9e9e9', //切换音频时的主题色，优先级低于audio.theme
    preload: 'none', //音频预加载，可选值: 'none', 'metadata', 'auto'
    mutex: true, //互斥，阻止多个播放器同时播放，当前播放器播放时暂停其他播放器
    lrcType: 3, //歌词格式，可选值：3（LRC文件歌词格式），1（JS字符串歌词格式）
    volume: 0.7, //默认音量，请注意播放器会记忆用户设置，用户手动设置音量后默认音量即失效
    fixed: false, //吸底模式（fixed:true），迷你模式（mini:true），普通模式（注释此行或者设置fixed:false）
    audio: [{
        name: '平凡之路',
        artist: '朴树',
        lrc: '/downloads/lrc/平凡之路-朴树.lrc',
        cover: 'http://p2.music.126.net/W_5XiCv3rGS1-J7EXpHSCQ==/18885211718782327.jpg?param=300x300',
        url: 'http://ws.stream.qqmusic.qq.com/C4000007nRJ74eQsF6.m4a?guid=942726817&vkey=64755357C5716983BEECE2981FEC3954F0DD03E7630EFF769D9C40C31F50D53E7D260CB63E22CE8F954B1144083716A1BABFC2093F8F1C14&uin=&fromtag=120032'
      },
      {
        name: '后会无期',
        artist: '徐良,汪苏泷',
        lrc: '/downloads/lrc/后会无期-G.E.M.邓紫棋.lrc',
        cover: 'http://p1.music.126.net/vpvPajo3kn88nHc7jUjeWQ==/5974746185758035.jpg?param=300x300',
        url: 'http://ws.stream.qqmusic.qq.com/C400004ECnYg0QNchK.m4a?guid=893251117&vkey=D9DCBB8C4A4CAF8550E97A2A574DE1231DFDD8BC0989CC724742C7B9831C4108E0A805A46E0F2B2A556228467C146B9C312E3E8B498614C7&uin=&fromtag=120032'
      }
    ]
  });

  // 实现切换音频时，根据音频的封面图片自适应主题色
  const colorThief = new ColorThief();
  const setTheme = (index) => {
    if (!ap.list.audios[index].theme) {
      colorThief.getColorAsync(ap.list.audios[index].cover, function(color) {
        ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index);
      });
    }
  };
  setTheme(ap.list.index);
  ap.on('listswitch', (data) => {
    setTheme(data.index);
  });
</script>



        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          Links
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9mYW50YXN5LWtlLmdpdGh1Yi5pbw==" title="https:&#x2F;&#x2F;fantasy-ke.github.io">SunBlog</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9mYW50YXN5LWtlLmdpdGh1Yi5pbw==" title="https:&#x2F;&#x2F;fantasy-ke.github.io">做梦的努力者</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL0ZhbnRhc3ktS2U=" title="https:&#x2F;&#x2F;github.com&#x2F;Fantasy-Ke">github</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9naXRlZS5jb20vZmFudGFzeV9rZQ==" title="https:&#x2F;&#x2F;gitee.com&#x2F;fantasy_ke">gitee</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly93d3cuY25ibG9ncy5jb20vZmFudGFzeS1rZQ==" title="https:&#x2F;&#x2F;www.cnblogs.com&#x2F;fantasy-ke">我的园子</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9hYnAuaW8v" title="https:&#x2F;&#x2F;abp.io&#x2F;">abp</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9hYnAuaW8vZG9jcy9sYXRlc3Qv" title="https:&#x2F;&#x2F;abp.io&#x2F;docs&#x2F;latest&#x2F;">abp doc</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9ibG9ncy5tYXNhc3RhY2suY29tLw==" title="https:&#x2F;&#x2F;blogs.masastack.com&#x2F;">masa blog</span>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        <div class="sidebar-inner sidebar-post-related">
          <div class="animated">
              <div class="links-of-blogroll-title"><i class="fa fa-signs-post fa-fw"></i>
    Related Posts
  </div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/59211/" rel="bookmark">
        <time class="popular-posts-time">2023-05-29</time>
        <br>
      RabbitMQ进阶
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/posts/25184/" rel="bookmark">
        <time class="popular-posts-time">2023-10-11</time>
        <br>
      .NET Core中RabbitMQ初识
      </a>
    </li>
  </ul>

          </div>
        </div>
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="https://blog.fantasyke.cn/posts/33842/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/avatar.png">
      <meta itemprop="name" content="Fantasy-ke">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Fantasy'Ke Blog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="RabbitMQ基础 | Fantasy'Ke Blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          RabbitMQ基础
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2023-05-26 22:36:00" itemprop="dateCreated datePublished" datetime="2023-05-26T22:36:00+08:00">2023-05-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2024-12-17 14:36:35" itemprop="dateModified" datetime="2024-12-17T14:36:35+08:00">2024-12-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%9D%82%E4%B8%83%E6%9D%82%E5%85%AB%E7%9A%84%E7%A0%96/" itemprop="url" rel="index"><span itemprop="name">杂七杂八的砖</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="Word count in article">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">Word count in article: </span>
      <span>3.3k</span>
    </span>
    <span class="post-meta-item" title="Reading time">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">Reading time &asymp;</span>
      <span>12 mins.</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><!-- more -->
<!-- - [主流消息中间件](#主流消息中间件)
  - [ActiveMQ](#activemq)
  - [Kafka](#kafka)
  - [RocketMQ](#rocketmq)
  - [RabbitMQ](#rabbitmq)
- [RabbitMQ核心概念及AMQP协议](#rabbitmq核心概念及amqp协议)
  - [为什么用RabbitMQ](#为什么用rabbitmq)
  - [RabbitMQ高性能的原因](#rabbitmq高性能的原因)
  - [什么是AMQP](#什么是amqp)
  - [AMQP核心概念](#amqp核心概念)
  - [RabbitMQ整体架构](#rabbitmq整体架构)
  - [RabbitMQ消息流转](#rabbitmq消息流转)
- [RabbitMQ常用命令](#rabbitmq常用命令)
  - [rabbitmq-server](#rabbitmq-server)
  - [rabbitmq-plugins](#rabbitmq-plugins)
- [消息生产与消费](#消息生产与消费)
  - [重要概念](#重要概念)
  - [创建项目](#创建项目)
  - [具体代码](#具体代码)
  - [运行测试](#运行测试)
- [Exchange–交换机](#exchange交换机)
  - [Exchange属性(交换机属性)](#exchange属性交换机属性)
  - [Direct Exchange](#direct-exchange)
    - [代码示例](#代码示例)
      - [创建项目](#创建项目-1)
      - [具体代码](#具体代码-1)
  - [Topic Exchange](#topic-exchange)
    - [代码示例](#代码示例-1)
      - [创建项目](#创建项目-2)
      - [具体代码](#具体代码-2)
  - [Fanout Exchange](#fanout-exchange)
    - [代码示例](#代码示例-2)
      - [创建项目](#创建项目-3)
      - [具体代码](#具体代码-3)
- [Binding–绑定](#binding绑定)
- [Queue–消息队列](#queue消息队列)
- [Message–消息](#message消息)
- [Virtual Hots–虚拟主机](#virtual-hots虚拟主机) -->
<!-- > 源码：[https://github.com/xiejiamiao/AllSamples](https://github.com/xiejiamiao/AllSamples) -->
<h1 id=""><a class="markdownIt-Anchor" href="#"></a> <a href="#%E4%B8%BB%E6%B5%81%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6" title="主流消息中间件"></a>主流消息中间件</h1>
<p><strong>衡量MQ指标：服务性能、数据存储、集群架构</strong></p>
<h2 id="-2"><a class="markdownIt-Anchor" href="#-2"></a> <a href="#ActiveMQ" title="ActiveMQ"></a>ActiveMQ</h2>
<ul>
<li>ActiveMQ是Apache出品，最流行的、能力最强劲的开源消息总线，并且它是一个完全支持JMS规范的消息中间件</li>
<li>其丰富的API、多种集群构建模式使得它称为业界老牌消息中间件，在中小型企业中应用广泛</li>
<li>性能比较一般，面对大数量高并发的情况容易出现堵塞、消息堆积过多导致延迟</li>
<li>适合并发量不高的场景</li>
</ul>
<p>ActiveMQ的集群模式：<br />
<img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.5fkly2609k.webp" alt="ActiveMQ集群模式.png" /></p>
<h2 id="-3"><a class="markdownIt-Anchor" href="#-3"></a> <a href="#Kafka" title="Kafka"></a>Kafka</h2>
<p>Kafka是LinkedIn开源的分布式发布-订阅消息系统，目前归属于Apache顶级项目。Kafka主要特点是基于Pull的模式来处理消息消费，追求高吞吐量(配置不高的单机服务器也能支撑每秒100K的吞吐数据量)，一开始的目的就是用于日志收集和传输。0.8版本开始支持复制，不支持事务，对消息的重复、丢失、错误没有严格要求，适合产生大量数据的互联网服务的数据收集业务</p>
<p>Kafka集群模式：<br />
<img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.syyxd7efm.webp" alt="Kafka集群模式.png" /></p>
<h2 id="-4"><a class="markdownIt-Anchor" href="#-4"></a> <a href="#RocketMQ" title="RocketMQ"></a>RocketMQ</h2>
<p>RocketMQ是阿里开源的消息中间件，目前也已经孵化为Apache顶级项目，它是纯Java开发，具有高吞吐量、高可用性、适合大规模分布式系统应用的特点。RocketMQ思路起源于Kafka，它对消息的可靠传输和事务性做了优化，目前在阿里集团被广泛应用于交易、充值、流计算、消息推送、日志流式处理、binglog分布等场景</p>
<p>RocketMQ集群模式：<br />
<img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.3uuuyl8nv6.webp" alt="RocketMQ集群模式.png" /></p>
<h2 id="-5"><a class="markdownIt-Anchor" href="#-5"></a> <a href="#RabbitMQ" title="RabbitMQ"></a>RabbitMQ</h2>
<p>RabbitMQ是使用Erlang语言开发的开源消息队列系统，基于AMQP协议来实现。AMQP的主要特征是面向消息、队列、路由（包括点对点和发布/订阅）、可靠性、安全。AMQP协议更多用在企业系统内，对数据一致性、稳定性和可靠性要求很高的场景，对性能和吞吐量的要求还在其次。</p>
<p>RabbitMQ高可用负载均衡集群模式：<br />
<img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.9dczeqgtfh.webp" alt="RbbitMQ高可用负载均衡集群.png" /></p>
<h1 id="-6"><a class="markdownIt-Anchor" href="#-6"></a> <a href="#RabbitMQ%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E5%8F%8AAMQP%E5%8D%8F%E8%AE%AE" title="RabbitMQ核心概念及AMQP协议"></a>RabbitMQ核心概念及AMQP协议</h1>
<p><strong>RabbitMQ</strong>是一个开源的消息代理和队列服务器，用来通过普通协议在完全不同的应用之间共享数据，RabbitMQ是使用Erlang语言来编写的，并且RabbitMQ是基于AMQP协议的。</p>
<h2 id="-7"><a class="markdownIt-Anchor" href="#-7"></a> <a href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E7%94%A8RabbitMQ" title="为什么用RabbitMQ"></a>为什么用RabbitMQ</h2>
<ul>
<li>与<code>SpringAMQP</code>完美的整合、API丰富</li>
<li>集群模式丰富，表达式配置，HA模式，镜像队列模型</li>
<li>保证数据不丢失的前提下做到高可靠性、可用性</li>
</ul>
<h2 id="-8"><a class="markdownIt-Anchor" href="#-8"></a> <a href="#RabbitMQ%E9%AB%98%E6%80%A7%E8%83%BD%E7%9A%84%E5%8E%9F%E5%9B%A0" title="RabbitMQ高性能的原因"></a>RabbitMQ高性能的原因</h2>
<p><strong>Erlang语言</strong>最初在于交换机领域的架构模式，这使得RabbitMQ在Broker之间进行数据交互的性能是非常优秀的</p>
<p><strong>Erlang</strong>的优点：有着与原生Socket一样的延迟</p>
<h2 id="-9"><a class="markdownIt-Anchor" href="#-9"></a> <a href="#%E4%BB%80%E4%B9%88%E6%98%AFAMQP" title="什么是AMQP"></a>什么是AMQP</h2>
<ul>
<li>AMQP全称：Advance Message Queuing Protocol（高级消息队列协议）</li>
<li>AMQP定义：具有现代特征的二进制协议。是一个提供统一消息的应用层标准高级消息队列协议，是应用层协议的一个开放标准，为面向消息的中间件设计。</li>
<li>AMQP协议模型：<br />
<img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.1lbuf3nraq.webp" alt="AMQP协议模型.png" /></li>
</ul>
<h2 id="-10"><a class="markdownIt-Anchor" href="#-10"></a> <a href="#AMQP%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5" title="AMQP核心概念"></a>AMQP核心概念</h2>
<ul>
<li><strong>Server</strong>：又称Broker，接受客户端的连接，实现AMQP实体服务</li>
<li><strong>Connection</strong>：连接，应用程序域Broker的网络连接</li>
<li><strong>Channel</strong>：网络信道，几乎所有的操作都在Channel中进行，Channel是进行消息读写的通道。客户端可建立多个Channel，每个Channel代表一个会话任务</li>
<li><strong>Message</strong>：消息，服务器和应用程序之间传送的数据，有<code>Properties</code>和<code>Body</code>组成，Properties可以对消息进行修饰，比如消息的优先级、延迟等高级特性；Body则是消息体内容</li>
<li><strong>Virtual host</strong>：虚拟主机，用于进行逻辑隔离，最上层的消息路由。一个Virtual host里面可以有若干个Exchange和Queue，同一个Virtual host里面不能有相同名称的Exchange和Queue</li>
<li><strong>Exchange</strong>：交换机，接受消息，根据路由键转发消息到绑定的队列</li>
<li><strong>Binding</strong>：Exchange和Queue之间的虚拟连接，bingding中可以包含routing key</li>
<li><strong>Routing key</strong>：一个路由规则，虚拟机可用它来确定如何路由一个特定消息</li>
<li><strong>Queue</strong>：也成为Message Queue，消息队列，保存消息并将它们转发给消费者</li>
</ul>
<h2 id="-11"><a class="markdownIt-Anchor" href="#-11"></a> <a href="#RabbitMQ%E6%95%B4%E4%BD%93%E6%9E%B6%E6%9E%84" title="RabbitMQ整体架构"></a>RabbitMQ整体架构</h2>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.86to64rpy0.webp" alt="RabbitMQ整体架构.png" /></p>
<h2 id="-12"><a class="markdownIt-Anchor" href="#-12"></a> <a href="#RabbitMQ%E6%B6%88%E6%81%AF%E6%B5%81%E8%BD%AC" title="RabbitMQ消息流转"></a>RabbitMQ消息流转</h2>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.7w6uczc9lq.webp" alt="RabbitMQ消息流转.png" /></p>
<h1 id="-13"><a class="markdownIt-Anchor" href="#-13"></a> <a href="#RabbitMQ%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4" title="RabbitMQ常用命令"></a>RabbitMQ常用命令</h1>
<h2 id="-14"><a class="markdownIt-Anchor" href="#-14"></a> <a href="#rabbitmq-server" title="rabbitmq-server"></a>rabbitmq-server</h2>
<ul>
<li>
<p>启动并后台运行：<code>rabbitmq-server start &amp;</code></p>
</li>
<li>
<p>停止服务：<code>rabbitmq-server stop</code></p>
<h2 id="-15"><a class="markdownIt-Anchor" href="#-15"></a> <a href="#rabbitmqctl" title="rabbitmqctl"></a>rabbitmqctl</h2>
</li>
<li>
<p>启动应用：<code>rabbitmqctl start_app</code></p>
</li>
<li>
<p>停止应用：<code>rabbitmqctl stop_app</code></p>
</li>
<li>
<p>节点状态：<code>rabbitmqctl status</code></p>
<h3 id="-16"><a class="markdownIt-Anchor" href="#-16"></a> <a href="#%E7%94%A8%E6%88%B7%E7%9B%B8%E5%85%B3" title="用户相关"></a>用户相关</h3>
</li>
<li>
<p>添加用户：<code>rabbitmqctl add_user username password</code></p>
</li>
<li>
<p>列出所有用户：<code>rabbitmqctl list_users</code></p>
</li>
<li>
<p>删除用户：<code>rabbitmqctl delete_user username</code></p>
</li>
<li>
<p>清除用户权限：<code>rabbitmqctl clear_permissions -p vhostpath username</code></p>
</li>
<li>
<p>列出用户权限：<code>rabbitmqctl list_user_permissions username</code></p>
</li>
<li>
<p>修改密码：<code>rabbitmqctl change_password username newpassword</code></p>
</li>
<li>
<p>设置用户权限：<code>rabbitmqctl set_permissions -p vhostpath username &quot;.*&quot; &quot;.*&quot; &quot;.*&quot;</code></p>
<h3 id="-17"><a class="markdownIt-Anchor" href="#-17"></a> <a href="#virtual-host%E7%9B%B8%E5%85%B3" title="virtual host相关"></a>virtual host相关</h3>
</li>
<li>
<p>创建虚拟主机：<code>rabbitmqctl add_vhost vhostpath</code></p>
</li>
<li>
<p>列出所有虚拟主机：<code>rabbitmqctl list_vhosts</code></p>
</li>
<li>
<p>列出虚拟主机上所有权限：<code>rabbitmqctl list_permissions -p vhostpath</code></p>
</li>
<li>
<p>删除虚拟主机：<code>rabbitmqctl delete_vhost vhostpath</code></p>
<h3 id="-18"><a class="markdownIt-Anchor" href="#-18"></a> <a href="#queue%E7%9B%B8%E5%85%B3" title="queue相关"></a>queue相关</h3>
</li>
<li>
<p>查看所有队列信息：<code>rabbitmqctl list_queues</code></p>
</li>
<li>
<p>清楚队列里的消息：<code>rabbitmqctl -p vhostpath purge_queue blue</code></p>
<h3 id="-19"><a class="markdownIt-Anchor" href="#-19"></a> <a href="#%E9%AB%98%E7%BA%A7%E6%93%8D%E4%BD%9C" title="高级操作"></a>高级操作</h3>
</li>
<li>
<p>移除所有数据：<code>rabbitmqctl reset</code>(要在<code>rabbitmqctl stop_app</code>之后使用)</p>
</li>
<li>
<p>组成集群命令：<code>rabbitmqctl join_cluster &lt;clusternode&gt; [--ram]</code></p>
</li>
<li>
<p>查看集群状态：<code>rabbitmqctl cluster_status</code></p>
</li>
<li>
<p>修改集群节点的存储形式：<code>rabbitmqctl change_cluster_node_type disc | ram</code></p>
</li>
<li>
<p>忘记节点(摘除节点)：<code>rabbitmqctl forget_cluster_node [--offline]</code></p>
</li>
<li>
<p>修改节点名称：<code>rabbitmqctl rename_cluster_node oldnode1 newnode1 [oldnode2] [newnode2...]</code></p>
</li>
</ul>
<h2 id="-20"><a class="markdownIt-Anchor" href="#-20"></a> <a href="#rabbitmq-plugins" title="rabbitmq-plugins"></a>rabbitmq-plugins</h2>
<ul>
<li>列出当前所有插件：<code>rabbitmq-plugins list</code></li>
<li>启动控制台：<code>rabbitmq-plugins enable rabbitmq_management</code></li>
</ul>
<h1 id="-21"><a class="markdownIt-Anchor" href="#-21"></a> <a href="#%E6%B6%88%E6%81%AF%E7%94%9F%E4%BA%A7%E4%B8%8E%E6%B6%88%E8%B4%B9" title="消息生产与消费"></a>消息生产与消费</h1>
<h2 id="-22"><a class="markdownIt-Anchor" href="#-22"></a> <a href="#%E9%87%8D%E8%A6%81%E6%A6%82%E5%BF%B5" title="重要概念"></a>重要概念</h2>
<ul>
<li><code>ConnectionFactory</code>：获取连接工厂</li>
<li><code>Connection</code>：一个连接</li>
<li><code>Channel</code>：数据通信信道，可发送和接收消息</li>
<li><code>Queue</code>：具体的消息存储队列</li>
<li><code>Producer</code>：消息生产者</li>
<li><code>Consumer</code>：消息消费者</li>
</ul>
<h2 id="-23"><a class="markdownIt-Anchor" href="#-23"></a> <a href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE" title="创建项目"></a>创建项目</h2>
<p>创建两个控制台应用程序，名字分别为：</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">BasicConsumer  </span><br><span class="line">BasicProducer  </span><br></pre></td></tr></table></figure>
<p>在两个项目分别通过nuget引入<code>RabbitMQ.Client</code></p>
<h2 id="-24"><a class="markdownIt-Anchor" href="#-24"></a> <a href="#%E5%85%B7%E4%BD%93%E4%BB%A3%E7%A0%81" title="具体代码"></a>具体代码</h2>
<p>BasicProducer.Program代码：</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;  </span><br><span class="line"><span class="keyword">using</span> System.Text;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">BasicProducer</span>  </span><br><span class="line">&#123;  </span><br><span class="line">  <span class="keyword">class</span> <span class="title">Program</span>  </span><br><span class="line">  &#123;  </span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>[] args</span>)</span>  </span><br><span class="line">    &#123;  </span><br><span class="line">      <span class="comment">// 1.创建一个ConnectionFactory  </span></span><br><span class="line">      <span class="keyword">var</span> connectionFactory = <span class="keyword">new</span> ConnectionFactory  </span><br><span class="line">      &#123;  </span><br><span class="line">        HostName = <span class="string">&quot;127.0.0.1&quot;</span>,  </span><br><span class="line">        Port = <span class="number">5672</span>,  </span><br><span class="line">        UserName = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        Password = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        VirtualHost = <span class="string">&quot;/&quot;</span>  </span><br><span class="line">      &#125;;  </span><br><span class="line">      <span class="comment">// 2.通过连接工厂创建连接  </span></span><br><span class="line">      <span class="keyword">using</span> (<span class="keyword">var</span> connection = connectionFactory.CreateConnection())  </span><br><span class="line">      &#123;  </span><br><span class="line">        <span class="comment">// 3.通过connection创建Channel  </span></span><br><span class="line">        <span class="keyword">var</span> channel = connection.CreateModel();  </span><br><span class="line">        <span class="comment">// 4.通过channel发送数据  </span></span><br><span class="line">        <span class="keyword">var</span> message = <span class="string">&quot;Hello RabbitMQ&quot;</span>;  </span><br><span class="line">        <span class="keyword">var</span> body = Encoding.UTF8.GetBytes(message);  </span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++)  </span><br><span class="line">        &#123;  </span><br><span class="line">          channel.BasicPublish(exchange: <span class="string">&quot;&quot;</span>, routingKey: <span class="string">&quot;test001&quot;</span>, basicProperties: <span class="literal">null</span>, body: body);  </span><br><span class="line">        &#125;  </span><br><span class="line">      &#125;  </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;发送完毕&quot;</span>);  </span><br><span class="line">    &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<p>BasicConsumer.Program代码</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;  </span><br><span class="line"><span class="keyword">using</span> System.Text;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client.Events;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">BasicConsumer</span>  </span><br><span class="line">&#123;  </span><br><span class="line"> <span class="keyword">class</span> <span class="title">Program</span>  </span><br><span class="line"> &#123;  </span><br><span class="line">  <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>[] args</span>)</span>  </span><br><span class="line">  &#123;  </span><br><span class="line">    <span class="comment">// 1.创建一个ConnectionFactory  </span></span><br><span class="line">    <span class="keyword">var</span> connectionFactory = <span class="keyword">new</span> ConnectionFactory  </span><br><span class="line">    &#123;  </span><br><span class="line">      HostName = <span class="string">&quot;127.0.0.1&quot;</span>,   </span><br><span class="line">      Port = <span class="number">5672</span>,  </span><br><span class="line">      UserName = <span class="string">&quot;admin&quot;</span>,   </span><br><span class="line">      Password = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">      VirtualHost = <span class="string">&quot;/&quot;</span>  </span><br><span class="line">    &#125;;  </span><br><span class="line">    <span class="comment">// 2.通过连接工厂创建连接  </span></span><br><span class="line">    <span class="keyword">using</span> (<span class="keyword">var</span> connection = connectionFactory.CreateConnection())  </span><br><span class="line">    &#123;  </span><br><span class="line">        <span class="comment">// 3.通过connection创建Channel  </span></span><br><span class="line">        <span class="keyword">var</span> channel = connection.CreateModel();  </span><br><span class="line">        <span class="comment">// 4.声明一个队列  </span></span><br><span class="line">        <span class="keyword">var</span> queue = channel.QueueDeclare(queue: <span class="string">&quot;test001&quot;</span>, durable: <span class="literal">true</span>, exclusive: <span class="literal">false</span>, autoDelete: <span class="literal">true</span>, arguments: <span class="literal">null</span>);  </span><br><span class="line">        <span class="comment">// 5.创建消费者  </span></span><br><span class="line">        <span class="keyword">var</span> consumer = <span class="keyword">new</span> EventingBasicConsumer(channel);  </span><br><span class="line">        <span class="comment">// 6.设置Channel  </span></span><br><span class="line">        channel.BasicConsume(queue: <span class="string">&quot;test001&quot;</span>, autoAck: <span class="literal">true</span>, consumer: consumer);  </span><br><span class="line">        <span class="comment">// 7.获取消息  </span></span><br><span class="line">        consumer.Received += (model, ea) =&gt;  </span><br><span class="line">        &#123;  </span><br><span class="line">          <span class="keyword">var</span> body = ea.Body;  </span><br><span class="line">          <span class="keyword">var</span> message = Encoding.UTF8.GetString(body.ToArray());  </span><br><span class="line">          Console.WriteLine(<span class="string">$&quot;接收到消息:<span class="subst">&#123;message&#125;</span>&quot;</span>);  </span><br><span class="line">        &#125;;  </span><br><span class="line">        Console.WriteLine(<span class="string">&quot;输入回车键键退出&quot;</span>);  </span><br><span class="line">        Console.ReadLine();  </span><br><span class="line">      &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<h2 id="-25"><a class="markdownIt-Anchor" href="#-25"></a> <a href="#%E8%BF%90%E8%A1%8C%E6%B5%8B%E8%AF%95" title="运行测试"></a>运行测试</h2>
<p><strong>注意：先运行Consumer，再运行Producer</strong>，可以看到Consumer端接收到了Producer端发出的5条消息</p>
<h1 id="-26"><a class="markdownIt-Anchor" href="#-26"></a> <a href="#Exchange%E2%80%93%E4%BA%A4%E6%8D%A2%E6%9C%BA" title="Exchange–交换机"></a>Exchange–交换机</h1>
<p>Exchange：接受消息，并根据路由键转发消息所绑定的队列<br />
<img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.4qrce1j5os.webp" alt="Exchange.png" /></p>
<h2 id="-27"><a class="markdownIt-Anchor" href="#-27"></a> <a href="#Exchange%E5%B1%9E%E6%80%A7-%E4%BA%A4%E6%8D%A2%E6%9C%BA%E5%B1%9E%E6%80%A7" title="Exchange属性(交换机属性)"></a>Exchange属性(交换机属性)</h2>
<ul>
<li>Name：交换机名字</li>
<li>Type：交换机类型 <code>direct</code>、<code>topic</code>、<code>fanout</code>、<code>headers</code></li>
<li>Durability：是否需要持久化，true为持久化</li>
<li>Auto Delete：当最后一个绑定到Exchange上的队列删除后，自动删除该Exchange</li>
<li>Internal：当前Exchange是否用于RabbitMQ内部使用，默认为false</li>
<li>Arguments：扩展参数，用于扩展AMQP协议自制定化使用</li>
</ul>
<h2 id="-28"><a class="markdownIt-Anchor" href="#-28"></a> <a href="#Direct-Exchange" title="Direct Exchange"></a>Direct Exchange</h2>
<p>所有发送到Direct Exchange的消息都会被转发到RouteKey中指定的Queue</p>
<p><strong>注意：Direct模式可以使用RabbitMQ自带的Exchange:default Exchange，所以不需要将Exchange进行任何绑定(bingding)操作，消息传递时，RouteKey必须完全匹配才会被队列接受，否则该消息会被抛弃</strong></p>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.3nrn35ngns.webp" alt="Direct Exchange.png" /></p>
<h3 id="-29"><a class="markdownIt-Anchor" href="#-29"></a> <a href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B" title="代码示例"></a>代码示例</h3>
<h4 id="-30"><a class="markdownIt-Anchor" href="#-30"></a> <a href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE-1" title="创建项目"></a>创建项目</h4>
<p>创建两个控制台应用程序</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">DirectExchange.Consumer  </span><br><span class="line">DirectExchange.Producer  </span><br></pre></td></tr></table></figure>
<p>再分别通过nuget引入<code>RabbitMQ.Client</code></p>
<h4 id="-31"><a class="markdownIt-Anchor" href="#-31"></a> <a href="#%E5%85%B7%E4%BD%93%E4%BB%A3%E7%A0%81-1" title="具体代码"></a>具体代码</h4>
<p>DirectExchange.Producer</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;  </span><br><span class="line"><span class="keyword">using</span> System.Text;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">DirectExchange.Producer</span>  </span><br><span class="line">&#123;  </span><br><span class="line">  <span class="keyword">class</span> <span class="title">Program</span>  </span><br><span class="line">  &#123;  </span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>[] args</span>)</span>  </span><br><span class="line">    &#123;  </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;Direct Exchange Producer Sample&quot;</span>);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> connectionFactory = <span class="keyword">new</span> ConnectionFactory()  </span><br><span class="line">      &#123;  </span><br><span class="line">        HostName = <span class="string">&quot;127.0.0.1&quot;</span>,  </span><br><span class="line">        Port = <span class="number">5672</span>,  </span><br><span class="line">        UserName = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        Password = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        VirtualHost = <span class="string">&quot;/&quot;</span>,  </span><br><span class="line">        AutomaticRecoveryEnabled = <span class="literal">true</span>,  </span><br><span class="line">        NetworkRecoveryInterval = TimeSpan.FromSeconds(<span class="number">3</span>)  </span><br><span class="line">      &#125;;  </span><br><span class="line">      <span class="keyword">using</span> (<span class="keyword">var</span> connection = connectionFactory.CreateConnection())  </span><br><span class="line">      &#123;  </span><br><span class="line">        <span class="keyword">using</span> (<span class="keyword">var</span> channel = connection.CreateModel())  </span><br><span class="line">        &#123;  </span><br><span class="line">          <span class="keyword">var</span> exchangeName = <span class="string">&quot;test_direct_exchange&quot;</span>;  </span><br><span class="line">          <span class="keyword">var</span> routingKey = <span class="string">&quot;test.direct&quot;</span>;  </span><br><span class="line">            </span><br><span class="line">          <span class="keyword">var</span> message = <span class="string">&quot;Hello World RabbitMQ For Direct Exchange&quot;</span>;  </span><br><span class="line">          <span class="keyword">var</span> body = Encoding.UTF8.GetBytes(message);  </span><br><span class="line">            </span><br><span class="line">          channel.BasicPublish(exchangeName, routingKey, <span class="literal">null</span>, body);  </span><br><span class="line">        &#125;  </span><br><span class="line">      &#125;  </span><br><span class="line">        </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;消息发送完毕&quot;</span>);  </span><br><span class="line">    &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<p>DirectExchange.Consumer</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;  </span><br><span class="line"><span class="keyword">using</span> System.Text;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client.Events;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">DirectExchange.Consumer</span>  </span><br><span class="line">&#123;  </span><br><span class="line">  <span class="keyword">class</span> <span class="title">Program</span>  </span><br><span class="line">  &#123;  </span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>[] args</span>)</span>  </span><br><span class="line">    &#123;  </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;Direct Exchange Consumer Sample&quot;</span>);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> connectionFactory = <span class="keyword">new</span> ConnectionFactory()  </span><br><span class="line">      &#123;  </span><br><span class="line">        HostName = <span class="string">&quot;127.0.0.1&quot;</span>,  </span><br><span class="line">        Port = <span class="number">5672</span>,  </span><br><span class="line">        UserName = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        Password = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        VirtualHost = <span class="string">&quot;/&quot;</span>,  </span><br><span class="line">        AutomaticRecoveryEnabled = <span class="literal">true</span>,  </span><br><span class="line">        NetworkRecoveryInterval = TimeSpan.FromSeconds(<span class="number">3</span>)  </span><br><span class="line">      &#125;;  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> connection = connectionFactory.CreateConnection();  </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> channel = connection.CreateModel();  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> exchangeName = <span class="string">&quot;test_direct_exchange&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> queueName = <span class="string">&quot;test_direct_queue&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> routingKey = <span class="string">&quot;test.direct&quot;</span>;  </span><br><span class="line">        </span><br><span class="line">      <span class="comment">// 声明一个交换机  </span></span><br><span class="line">      channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Direct, durable: <span class="literal">true</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);  </span><br><span class="line">      <span class="comment">// 声明一个队列  </span></span><br><span class="line">      channel.QueueDeclare(queue: queueName, durable: <span class="literal">true</span>, exclusive: <span class="literal">false</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);  </span><br><span class="line">      <span class="comment">// 建立一个绑定关系  </span></span><br><span class="line">      channel.QueueBind(queue:queueName,exchange:exchangeName,routingKey:routingKey);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> consumer = <span class="keyword">new</span> EventingBasicConsumer(channel);  </span><br><span class="line">      channel.BasicConsume(queue: queueName, autoAck: <span class="literal">true</span>, consumer);  </span><br><span class="line">      consumer.Received += (model, ea) =&gt;  </span><br><span class="line">      &#123;  </span><br><span class="line">        <span class="keyword">var</span> body = ea.Body;  </span><br><span class="line">        <span class="keyword">var</span> message = Encoding.UTF8.GetString(body.ToArray());  </span><br><span class="line">        Console.WriteLine(<span class="string">$&quot;接收到消息：<span class="subst">&#123;message&#125;</span>&quot;</span>);  </span><br><span class="line">      &#125;;  </span><br><span class="line">        </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;输入回车键退出...&quot;</span>);  </span><br><span class="line">      Console.ReadLine();  </span><br><span class="line">    &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<h2 id="-32"><a class="markdownIt-Anchor" href="#-32"></a> <a href="#Topic-Exchange" title="Topic Exchange"></a>Topic Exchange</h2>
<p>所有发送到Topic Exchange的消息都会被转发到所有关系RouteKey中指定Topic的Queue上</p>
<p>Exchange将RouteKey和某个Topic进行模糊匹配，此时队列需要绑定一个Topic</p>
<p><em>注意：可以使用通配符进行模糊匹配</em></p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">符号<span class="string">&quot;#&quot;</span>匹配一个或多个词  </span><br><span class="line">符号<span class="string">&quot;\*&quot;</span>匹配不多不少一个词  </span><br><span class="line">例如：<span class="string">&quot;log.#&quot;</span> 能够匹配到 <span class="string">&quot;log.info.oa&quot;</span>  </span><br><span class="line"> <span class="string">&quot;log.\*&quot;</span> 只能够匹配到 <span class="string">&quot;log.error&quot;</span>  </span><br></pre></td></tr></table></figure>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.7zqgap6y0v.webp" alt="Topic Exchange.png" /></p>
<h3 id="-33"><a class="markdownIt-Anchor" href="#-33"></a> <a href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B-1" title="代码示例"></a>代码示例</h3>
<h4 id="-34"><a class="markdownIt-Anchor" href="#-34"></a> <a href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE-2" title="创建项目"></a>创建项目</h4>
<p>创建两个控制台应用程序</p>
<p>TopicExchange.Consumer<br />
TopicExchange.Producer</p>
<p>再分别通过nuget引入<code>RabbitMQ.Client</code></p>
<h4 id="-35"><a class="markdownIt-Anchor" href="#-35"></a> <a href="#%E5%85%B7%E4%BD%93%E4%BB%A3%E7%A0%81-2" title="具体代码"></a>具体代码</h4>
<p>TopicExchange.Producer</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;  </span><br><span class="line"><span class="keyword">using</span> System.Text;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">TopicExchange.Producer</span>  </span><br><span class="line">&#123;  </span><br><span class="line">  <span class="keyword">class</span> <span class="title">Program</span>  </span><br><span class="line">  &#123;  </span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>\[\] args</span>)</span>  </span><br><span class="line">    &#123;  </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;\*\*\*\* Topic Exchange Producer Sample \*\*\*\*&quot;</span>);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> connectionFactory = <span class="keyword">new</span> ConnectionFactory()  </span><br><span class="line">      &#123;  </span><br><span class="line">        HostName = <span class="string">&quot;127.0.0.1&quot;</span>,  </span><br><span class="line">        Port = <span class="number">5672</span>,  </span><br><span class="line">        UserName = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        Password = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        VirtualHost = <span class="string">&quot;/&quot;</span>,  </span><br><span class="line">        AutomaticRecoveryEnabled = <span class="literal">true</span>,  </span><br><span class="line">        NetworkRecoveryInterval = TimeSpan.FromSeconds(<span class="number">3</span>)  </span><br><span class="line">      &#125;;  </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> connection = connectionFactory.CreateConnection();  </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> channel = connection.CreateModel();  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> exchangeName = <span class="string">&quot;test_topic_exchange&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> routingKey1 = <span class="string">&quot;user.save&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> routingKey2 = <span class="string">&quot;user.update&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> routingKey3 = <span class="string">&quot;user.delete.abc&quot;</span>;  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> message = <span class="string">&quot;Hello World RabbitMQ For Topic Exchange Message&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> body = Encoding.UTF8.GetBytes(message);  </span><br><span class="line">        </span><br><span class="line">      channel.BasicPublish(exchange: exchangeName, routingKey: routingKey1, basicProperties: <span class="literal">null</span>, body);  </span><br><span class="line">      channel.BasicPublish(exchange: exchangeName, routingKey: routingKey2, basicProperties: <span class="literal">null</span>, body);  </span><br><span class="line">      channel.BasicPublish(exchange: exchangeName, routingKey: routingKey3, basicProperties: <span class="literal">null</span>, body);  </span><br><span class="line">        </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;消息发送完毕&quot;</span>);  </span><br><span class="line">    &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<p>TopicExchange.Consumer</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;  </span><br><span class="line"><span class="keyword">using</span> System.Text;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client.Events;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">TopicExchange.Consumer</span>  </span><br><span class="line">&#123;  </span><br><span class="line">  <span class="keyword">class</span> <span class="title">Program</span>  </span><br><span class="line">  &#123;  </span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>[] args</span>)</span>  </span><br><span class="line">    &#123;  </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;\*\*\*\* Topic Exchange Consumer Sample \*\*\*\*&quot;</span>);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> connectionFactory = <span class="keyword">new</span> ConnectionFactory()  </span><br><span class="line">      &#123;  </span><br><span class="line">        HostName = <span class="string">&quot;127.0.0.1&quot;</span>,  </span><br><span class="line">        Port = <span class="number">5672</span>,  </span><br><span class="line">        UserName = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        Password = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        VirtualHost = <span class="string">&quot;/&quot;</span>,  </span><br><span class="line">        AutomaticRecoveryEnabled = <span class="literal">true</span>,  </span><br><span class="line">        NetworkRecoveryInterval = TimeSpan.FromSeconds(<span class="number">3</span>)  </span><br><span class="line">      &#125;;  </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> connection = connectionFactory.CreateConnection();  </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> channel = connection.CreateModel();  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> exchangeName = <span class="string">&quot;test_topic_exchange&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> queueName = <span class="string">&quot;test_topic_queue&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> routingKey = <span class="string">&quot;user.\*&quot;</span>;  </span><br><span class="line">      <span class="comment">// 声明交换机  </span></span><br><span class="line">      channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Topic, durable: <span class="literal">true</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);  </span><br><span class="line">      <span class="comment">// 声明队列  </span></span><br><span class="line">      channel.QueueDeclare(queue: queueName, durable: <span class="literal">false</span>, exclusive: <span class="literal">false</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);  </span><br><span class="line">      <span class="comment">// 建立绑定关系  </span></span><br><span class="line">      channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routingKey);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> consumer = <span class="keyword">new</span> EventingBasicConsumer(channel);  </span><br><span class="line">      channel.BasicConsume(queue: queueName, autoAck: <span class="literal">true</span>, consumer: consumer);  </span><br><span class="line">      consumer.Received += (model, ea) =&gt;  </span><br><span class="line">      &#123;  </span><br><span class="line">      <span class="keyword">var</span> body = ea.Body;  </span><br><span class="line">      <span class="keyword">var</span> message = Encoding.UTF8.GetString(body.ToArray());  </span><br><span class="line">        </span><br><span class="line">      Console.WriteLine(<span class="string">$&quot;接收到消息：<span class="subst">&#123;message&#125;</span>   RoutingKey=<span class="subst">&#123;ea.RoutingKey&#125;</span>&quot;</span>);  </span><br><span class="line">    &#125;;  </span><br><span class="line">      </span><br><span class="line">    Console.WriteLine(<span class="string">&quot;输入回车退出...&quot;</span>);  </span><br><span class="line">    Console.ReadLine();  </span><br><span class="line">    &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<h2 id="-36"><a class="markdownIt-Anchor" href="#-36"></a> <a href="#Fanout-Exchange" title="Fanout Exchange"></a>Fanout Exchange</h2>
<p>不处理路由键，只需要简单的队列绑定到交换机上，发送到交换机上的消息都会被转发到与该交换机绑定的所有队列上，<strong>Fanout交换机转发消息是最快的</strong></p>
<p><img src="https://fantasy-ke.github.io/picx-images-hosting/rabbitMQ/image.491apgi7ud.webp" alt="Fanout Exchange.png" /></p>
<h3 id="-37"><a class="markdownIt-Anchor" href="#-37"></a> <a href="#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B-2" title="代码示例"></a>代码示例</h3>
<h4 id="-38"><a class="markdownIt-Anchor" href="#-38"></a> <a href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE-3" title="创建项目"></a>创建项目</h4>
<p>创建两个控制台应用程序</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">FanoutExchange.Consumer  </span><br><span class="line">FanoutExchange.Producer  </span><br></pre></td></tr></table></figure>
<p>再分别通过nuget引入<code>RabbitMQ.Client</code></p>
<h4 id="-39"><a class="markdownIt-Anchor" href="#-39"></a> <a href="#%E5%85%B7%E4%BD%93%E4%BB%A3%E7%A0%81-3" title="具体代码"></a>具体代码</h4>
<p>FanoutExchange.Producer</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;  </span><br><span class="line"><span class="keyword">using</span> System.Text;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">FanoutExchange.Producer</span>  </span><br><span class="line">&#123;  </span><br><span class="line">  <span class="keyword">class</span> <span class="title">Program</span>  </span><br><span class="line">  &#123;  </span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>\[\] args</span>)</span>  </span><br><span class="line">    &#123;  </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;\*\*\*\* Fanout Exchange Producer Sample \*\*\*\*&quot;</span>);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> connectionFactory = <span class="keyword">new</span> ConnectionFactory()  </span><br><span class="line">      &#123;  </span><br><span class="line">        HostName = <span class="string">&quot;127.0.0.1&quot;</span>,  </span><br><span class="line">        Port = <span class="number">5672</span>,  </span><br><span class="line">        UserName = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        Password = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        VirtualHost = <span class="string">&quot;/&quot;</span>,  </span><br><span class="line">        AutomaticRecoveryEnabled = <span class="literal">true</span>,  </span><br><span class="line">        NetworkRecoveryInterval = TimeSpan.FromSeconds(<span class="number">3</span>)  </span><br><span class="line">      &#125;;  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> connection = connectionFactory.CreateConnection();  </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> channel = connection.CreateModel();  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> exchangeName = <span class="string">&quot;test_fanout_exchange&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> message = <span class="string">&quot;Hello World RabbitMQ For Fanout Exchange&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> body = Encoding.UTF8.GetBytes(message);  </span><br><span class="line">      channel.BasicPublish(exchange: exchangeName, routingKey: <span class="string">&quot;&quot;</span>, basicProperties: <span class="literal">null</span>, body: body);  </span><br><span class="line">        </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;消息发送完毕&quot;</span>);  </span><br><span class="line">    &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<p>FanoutExchange.Consumer</p>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;  </span><br><span class="line"><span class="keyword">using</span> System.Text;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client;  </span><br><span class="line"><span class="keyword">using</span> RabbitMQ.Client.Events;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">FanoutExchange.Consumer</span>  </span><br><span class="line">&#123;  </span><br><span class="line">  <span class="keyword">class</span> <span class="title">Program</span>  </span><br><span class="line">  &#123;  </span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="built_in">string</span>\[\] args</span>)</span>  </span><br><span class="line">    &#123;  </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;\*\*\*\* Fanout Exchange Consumer Sample \*\*\*\*&quot;</span>);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> connectionFactory = <span class="keyword">new</span> ConnectionFactory()  </span><br><span class="line">      &#123;  </span><br><span class="line">        HostName = <span class="string">&quot;127.0.0.1&quot;</span>,  </span><br><span class="line">        Port = <span class="number">5672</span>,  </span><br><span class="line">        UserName = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        Password = <span class="string">&quot;admin&quot;</span>,  </span><br><span class="line">        VirtualHost = <span class="string">&quot;/&quot;</span>,  </span><br><span class="line">        AutomaticRecoveryEnabled = <span class="literal">true</span>,  </span><br><span class="line">        NetworkRecoveryInterval = TimeSpan.FromSeconds(<span class="number">3</span>)  </span><br><span class="line">      &#125;;  </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> connection = connectionFactory.CreateConnection();  </span><br><span class="line">      <span class="keyword">using</span> <span class="keyword">var</span> channel = connection.CreateModel();  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> exchangeName = <span class="string">&quot;test_fanout_exchange&quot;</span>;  </span><br><span class="line">      <span class="keyword">var</span> queueName = <span class="string">&quot;test_fanout_queue&quot;</span>;  </span><br><span class="line">      <span class="comment">// 声明  </span></span><br><span class="line">      channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Fanout, durable: <span class="literal">true</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);  </span><br><span class="line">      channel.QueueDeclare(queue: queueName, durable: <span class="literal">true</span>, exclusive: <span class="literal">false</span>, autoDelete: <span class="literal">false</span>, arguments: <span class="literal">null</span>);  </span><br><span class="line">      channel.QueueBind(queue:queueName,exchange:exchangeName,<span class="string">&quot;&quot;</span>);  </span><br><span class="line">        </span><br><span class="line">      <span class="keyword">var</span> consumer = <span class="keyword">new</span> EventingBasicConsumer(channel);  </span><br><span class="line">      channel.BasicConsume(queue: queueName, autoAck: <span class="literal">true</span>, consumer);  </span><br><span class="line">      consumer.Received += (model, ea) =&gt;  </span><br><span class="line">      &#123;  </span><br><span class="line">        <span class="keyword">var</span> body = ea.Body;  </span><br><span class="line">        <span class="keyword">var</span> message = Encoding.UTF8.GetString(body.ToArray());  </span><br><span class="line">        Console.WriteLine(<span class="string">$&quot;接收到消息：<span class="subst">&#123;message&#125;</span>&quot;</span>);  </span><br><span class="line">      &#125;;  </span><br><span class="line">      Console.WriteLine(<span class="string">&quot;输入回车退出...&quot;</span>);  </span><br><span class="line">      Console.ReadLine();  </span><br><span class="line">    &#125;  </span><br><span class="line">  &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<h1 id="-40"><a class="markdownIt-Anchor" href="#-40"></a> <a href="#Binding%E2%80%93%E7%BB%91%E5%AE%9A" title="Binding–绑定"></a>Binding–绑定</h1>
<ul>
<li>Exchange和Exchange、Queue之间的连接关系</li>
<li>Binding中可以包含RoutingKey或参数</li>
</ul>
<h1 id="-41"><a class="markdownIt-Anchor" href="#-41"></a> <a href="#Queue%E2%80%93%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97" title="Queue–消息队列"></a>Queue–消息队列</h1>
<ul>
<li>消息队列，实际存储消息数据</li>
<li>Durability：是否持久化，Durable：是，Transient：否</li>
<li>Auto Delete：如果是yes，则代表当最后一个监听被移除之后，该queue会被自动删除</li>
</ul>
<h1 id="-42"><a class="markdownIt-Anchor" href="#-42"></a> <a href="#Message%E2%80%93%E6%B6%88%E6%81%AF" title="Message–消息"></a>Message–消息</h1>
<ul>
<li>服务器和应用程序之间传送的数据</li>
<li>本质上就是一段数据，由Properties和Payload(Body)组成</li>
<li>常用属性：delivery mode、headers（自定义属性）</li>
<li>content_type、content_encoding、priority(优先级)</li>
<li>correlation_id（消息唯一ID）、reply_to（消息处理失败了返回哪个队列）、expiration（消息过期时间，多久时间没被消费就过期）、message_id</li>
<li>timestamp、type、user_id、app_id、cluster_id</li>
</ul>
<h1 id="-43"><a class="markdownIt-Anchor" href="#-43"></a> <a href="#Virtual-Hots%E2%80%93%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA" title="Virtual Hots–虚拟主机"></a>Virtual Hots–虚拟主机</h1>
<ul>
<li>虚拟地址，用于进行逻辑隔离，最上层的消息路由</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">







<div class="license">
  <div class="license-title">RabbitMQ基础</div>
  <div class="license-link">
    <a href="https://blog.fantasyke.cn/posts/33842/">https://blog.fantasyke.cn/posts/33842/</a>
  </div>
  <div class="license-meta">
    <div class="license-meta-item">
      <div class="license-meta-title">本文作者</div>
      <div class="license-meta-text">
          Fantasy-ke
      </div>
    </div>
      <div class="license-meta-item">
        <div class="license-meta-title">发布于</div>
        <div class="license-meta-text">
          2023-05-26
        </div>
      </div>
      <div class="license-meta-item">
        <div class="license-meta-title">更新于</div>
        <div class="license-meta-text">
          2024-12-17
        </div>
      </div>
    <div class="license-meta-item">
      <div class="license-meta-title">许可协议</div>
      <div class="license-meta-text">
          禁止转载引用
      </div>
    </div>
  </div>
  <div class="license-statement">
      如需转载或引用本文，请先获得作者授权！
  </div>
</div>
          <div class="reward-container">
  <div>Buy me a coffee</div>
  <button>
    Donate
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="Fantasy-ke WeChat Pay">
        <span>WeChat Pay</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>Welcome to my other publishing channels</span>

  <div class="social-list">

      <div class="social-item">
          <a target="_blank" class="social-link" href="https://github.com/fantasy-ke">
            <span class="icon">
              <i class="fab fa-github"></i>
            </span>

            <span class="label">GitHub</span>
          </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97/" rel="tag"><i class="fa fa-tag"></i> 消息队列</a>
              <a href="/tags/RabbitMQ/" rel="tag"><i class="fa fa-tag"></i> RabbitMQ</a>
          </div>

        
  <div class="social-like a2a_kit a2a_kit_size_32 a2a_default_style">
    <a class="a2a_dd" target="_blank" rel="noopener" href="https://www.addtoany.com/share"></a>
      <a class="a2a_button_facebook"></a>
      <a class="a2a_button_twitter"></a>
      <a class="a2a_button_telegram"></a>
      <a class="a2a_button_wechat"></a>
  </div>

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/59211/" rel="prev" title="RabbitMQ进阶">
                  <i class="fa fa-angle-left"></i> RabbitMQ进阶
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/24227/" rel="next" title="多线程">
                  多线程 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2021 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-solid fa-t-rex"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Fantasy-ke</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>Word count total: </span>
    <span title="Word count total">72k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>Reading time total &asymp;</span>
    <span title="Reading time total">4:21</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div> 
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment-precise-range-plugin@1.3.0/moment-precise-range.min.js"></script>
<script>
  var now = new Date();
  function timer() {
    var grt= new Date("03/12/2022 13:14:21");//此处修改你的建站时间或者网站上线时间
    now.setTime(now.getTime()+250);
    days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
    hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
    if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
    seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
    var ages = `${dnum}天${hnum}小时${mnum}分${snum}秒`;
    div.innerHTML = `本站已安全运行 ${ages} 🐶`;
  }
  var div = document.createElement("div");
  //插入到copyright之后
  var copyright = document.querySelector(".busuanzi-count");
  document.querySelector(".footer-inner").insertBefore(div, copyright.nextSibling);
  timer();
  setInterval("timer()",1000)
</script>


<div class="footer-menu">
    <a style="color: chocolate;" href="/tags/">文章标签</a> · <a style="color: chocolate;"  href="/policy/">网站政策</a> · <a style="color: chocolate;"  href="/photos/">文章相册</a>
</div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnRhc3kta2U=" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.31/fancybox/fancybox.umd.js" integrity="sha256-a+H7FYzJv6oU2hfsfDGM2Ohw/cR9v+hPfxHCLdmCrE8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>


  <script src="/js/third-party/addtoany.js"></script>

  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>

<script>
var options = {
  bottom: '64px',
  right: 'unset',
  left: '32px',
  time: '0.5s',
  mixColor: 'transparent',
  backgroundColor: 'transparent',
  buttonColorDark: '#100f2c',
  buttonColorLight: '#fff',
  saveInCookies: true,
  label: '🌓',
  autoMatchOsTheme: true
}
const darkmode = new Darkmode(options);
window.darkmode = darkmode;
darkmode.showWidget();
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"fantasy-ke","repo":"fantasy-ke.github.io","client_id":"Ov23li8eCFrhnzfiGx5T","client_secret":"ce23581c2059392ebd00afd08d5ba43f767fd1b1","admin_user":"fantasy-ke","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","labels":"gitalk","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.min.js","integrity":"sha256-MVK9MGD/XJaGyIghSVrONSnoXoGh3IFxLw0zfvzpxR4="},"path_md5":"8ddb0a7d90a2a0e1ea3659099181b9de"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>


<script src="/js/minigrid.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script data-pjax type="text/javascript">
var album = document.querySelector(".album");
if (album) {
  // 相册列表 JSON 数据
  var imgDataPath = album.getAttribute('json-src');
  // 照片存储路径
  var imgPath = album.getAttribute('photo-src');
  // 最多显示数量
  var imgMaxNum = 50;
  // 获取窗口大小以决定图片宽度
  var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  var imageWidth;

  if (windowWidth < 768) {
    imageWidth = 145; // 移动端图片宽度
  } else {
    imageWidth = 235;
  }

  // 生成相册
  var linkDataPath = imgDataPath;
  var photo = {
    page: 1,
    offset: imgMaxNum,
    init: function () {
      var that = this;
      $.getJSON(linkDataPath, function (data) {
          that.render(that.page, data);
      });
    },
    render: function (page, data) {
      var begin = (page - 1) * this.offset;
      var end = page * this.offset;
      if (begin >= data.length) return;
      var imgNameWithPattern, imgName, imageSize, imageX, imageY, li = "";
      for (var i = begin; i < end && i < data.length; i++) {
        imgNameWithPattern = data[i].path;
        imgName = data[i].name;
        imageSize = data[i].size;
        li += '<div class="card" style="width:' + imageWidth + 'px" >';
        li += '<div class="album-photo" style="height:' + imageWidth + 'px" >';
        li += '<a class="fancybox fancybox.image" href="' + imgPath + imgNameWithPattern + '" itemscope="" itemtype="http://schema.org/ImageObject" itemprop="url" data-fancybox="group" rel="group" data-caption="' + imgName + '" title="' +  imgName + '">';
        li += '<img data-src="' + imgPath + imgNameWithPattern + '" src="' + imgPath + imgNameWithPattern + '" alt="' +  imgName + '" data-loaded="true">';
        li += '</a>';
        li += '</div>';
        li += '</div>';
      }
      album.insertAdjacentHTML('beforeend', li);
      this.minigrid();
    },
    minigrid: function () {
      var grid = new Minigrid({
        container: '.album',
        item: '.card',
        gutter: 12
      });
      grid.mount();
      window.addEventListener('resize', function () {
        grid.mount();
      });
    }
  };
  photo.init();
}
</script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":170,"height":330},"mobile":{"show":true},"react":{"opacity":0.7},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
